package com.yimils.core.goods.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yimils.core.goods.dto.CategoryAddReq;
import com.yimils.core.goods.dto.CategoryUpdateReq;
import com.yimils.core.goods.entity.CategoryEntity;
import com.yimils.core.goods.mapper.CategoryMapper;
import com.yimils.core.goods.serivce.CategoryService;
import com.yimils.core.goods.vo.CategoryRes;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEntity> implements CategoryService {

    private String getCategoryCode() {
        String str = "HT";
        CategoryEntity entity = getBaseMapper().getCategoryOne();
        if (entity == null) {
            return "HT001";
        }
        return str + String.format("%03d", Integer.valueOf(entity.getCategoryCode().replace(str, "")) + 1);
    }

    @Override
    public Boolean saveCategory(CategoryAddReq categoryAddReq) {

        CategoryEntity entity = new CategoryEntity();
        entity.setCategoryCode(getCategoryCode());
        entity.setParentId(categoryAddReq.getParentId() == null ? 0 : categoryAddReq.getParentId());
        entity.setRank(categoryAddReq.getRank());
        entity.setCategoryName(categoryAddReq.getCategoryName());
        entity.setRemarks(categoryAddReq.getRemarks());
        entity.setState(Boolean.FALSE);
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        return save(entity);
    }

    @Override
    public Boolean updateCategory(CategoryUpdateReq categoryUpdateReq) {
        QueryWrapper<CategoryEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(CategoryEntity::getId, categoryUpdateReq.getCategoryId()).eq(CategoryEntity::getState, Boolean.FALSE);
        CategoryEntity entity = getOne(qw);
        entity.setRank(categoryUpdateReq.getRank());
        entity.setCategoryName(categoryUpdateReq.getCategoryName());
        entity.setRemarks(categoryUpdateReq.getRemarks());
        entity.setUpdateTime(LocalDateTime.now());
        return saveOrUpdate(entity);
    }

    @Override
    public Boolean deleteCategory(List<Long> categoryIds) {
        QueryWrapper<CategoryEntity> qw = new QueryWrapper<>();
        qw.lambda().in(CategoryEntity::getId, categoryIds).eq(CategoryEntity::getState, Boolean.FALSE);
        List<CategoryEntity> categoryEntities = list(qw);
        if (CollectionUtils.isEmpty(categoryEntities)) {
            return Boolean.FALSE;
        }
        categoryEntities.forEach(o -> {
            o.setUpdateTime(LocalDateTime.now());
            o.setState(Boolean.TRUE);
        });
        return saveOrUpdateBatch(categoryEntities);
    }

    @Override
    public List<CategoryRes> listCategory() {
        QueryWrapper<CategoryEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(CategoryEntity::getState, Boolean.FALSE);
        List<CategoryEntity> categoryEntities = list(qw);
        return categoryEntities.stream().map(o -> {
            CategoryRes categoryRes = new CategoryRes();
            categoryRes.setRemarks(o.getRemarks());
            categoryRes.setCategoryId(o.getId());
            categoryRes.setCategoryName(o.getCategoryName());
            categoryRes.setRank(o.getRank());
            return categoryRes;
        }).collect(Collectors.toList());

    }
}
